﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


public static class DateTimeUtils
{
    /// <summary>
    /// 时间戳起始日期（注意这个是本地时间）
    /// </summary>
    public static readonly DateTime TimestampStart = new(1970, 1, 1, 0, 0, 0, 0);

    /// <summary>
    /// 转换为时间戳
    /// </summary>
    /// <param name="dateTime"></param>
    /// <param name="milliseconds">是否使用毫秒</param>
    /// <returns></returns>
    public static long ToTimestamp(this DateTime dateTime, bool milliseconds = false)
    {

        var timestamp = dateTime.ToUniversalTime() - TimestampStart;
        return (long)(milliseconds ? timestamp.TotalMilliseconds : timestamp.TotalSeconds);
    }





    /// <summary>
    /// 获取周几
    /// </summary>
    /// <param name="datetime"></param>
    /// <returns></returns>
    public static string GetWeekName(this DateTime datetime)
    {
        var day = (int)datetime.DayOfWeek;
        var week = new string[] { "周日", "周一", "周二", "周三", "周四", "周五", "周六" };
        return week[day];
    }


    public static IList<DayOfWeek> TranToWeek(string[] weeks)
    {
        List<DayOfWeek> dayOfWeeks = new List<DayOfWeek>();
        foreach (var item in weeks)
        {
            if (item == "周一") dayOfWeeks.Add(DayOfWeek.Monday);
            else if (item == "周二") dayOfWeeks.Add(DayOfWeek.Tuesday);
            else if (item == "周三") dayOfWeeks.Add(DayOfWeek.Wednesday);
            else if (item == "周四") dayOfWeeks.Add(DayOfWeek.Thursday);
            else if (item == "周五") dayOfWeeks.Add(DayOfWeek.Friday);
            else if (item == "周六") dayOfWeeks.Add(DayOfWeek.Saturday);
            else if (item == "周日") dayOfWeeks.Add(DayOfWeek.Sunday);
        }
        return dayOfWeeks;
    }

    /// <summary>
    /// 获取当前月的月末日期
    /// </summary>
    /// <returns></returns>
    public static DateTime GetEndMonth()
    {
        int span = Convert.ToInt32(System.DateTime.Now.Day);
        System.DateTime dateEnd = System.DateTime.Now.AddMonths(1).AddDays(-span);
        return dateEnd.Date;
    }


    /// <summary>
    /// 获取当前月初日期
    /// </summary>
    /// <returns></returns>
    public static DateTime GetStartMonth()
    {
        int span = Convert.ToInt32(System.DateTime.Now.Day);
        span = span - 1;
        System.DateTime dateStart = System.DateTime.Now.AddDays(-span);
        return dateStart.Date;
    }
}